source("https://raw.githubusercontent.com/traffordDataLab/assets/601e80334e0d78dfe913685561196b8b6fc278a7/theme/ggplot2/theme_lab.R")

theme_nath <- function () { 
  theme_grey(base_size = 11.5, base_family = "Roboto") %+replace% 
    theme(
      # add padding to the plot
      plot.margin = unit(rep(0.5, 4), "cm"),
      # remove the plot background and border
      plot.background = element_blank(),
      panel.background = element_blank(),
      panel.border = element_blank(),
      # make the legend and strip background transparent
      legend.background = element_rect(fill = "transparent", colour = NA),
      legend.key = element_rect(fill = "transparent", colour = NA),
      strip.background = element_rect(fill = "transparent", colour = NA),
      # add light, dotted major grid lines only
      panel.grid.major = element_line(linetype = "dotted", colour = "#757575", size = 0.3),
      panel.grid.minor = element_blank(),
      # remove the axis tick marks and hide axis lines
      axis.ticks = element_blank(),
      axis.line = element_line(color = "#FFFFFF", size = 0.3),
      # modify the bottom margins of the title and subtitle
      plot.title = element_text(size = 18, colour = "#757575", hjust = 0, margin = margin(b = 4)),
      plot.subtitle = element_text(size = 12, colour = "#757575", hjust = 0, margin = margin(b = 10)),
      # add padding to the caption
      plot.caption = element_text(size = 10, colour = "#757575", hjust = 1, margin = margin(t = 15)),
      # change to Open Sans for axes titles, tick labels, legend title and legend key, and strip text
      axis.title = element_text(family = "Open Sans", size = 11, colour = "#757575", face = "plain", hjust = 1),
      axis.text = element_text(family = "Open Sans", size = 10, colour = "#757575", face = "plain"),
      legend.title = element_text(size = 12, colour = "#757575"),
      legend.text = element_text(size = 10, colour = "#757575"),
      strip.text = element_text(family = "Open Sans", size = 12, colour = "#757575", face = "plain")
    )
}

#lista de cores da paleta
#Set2 = c("#66C2A5", "#FC8D62", "#8DA0CB", "#E78AC3", "#A6D854", "#FFD92F", "#E5C494", "#B3B3B3")
library(tidyverse)
library(patchwork)

Introdução

Este arquivo concentra as visualizações relacionadas aos dados do gapminder.

Dados

Serão utilizados dados da iniciativa gapminder, disponibilizada em gapminder::gapminder e dslabs::gapminder, tendo que neste último algumas informações a mais são disponibilizadas.

dslabs

Esta base possui vários anos a mais que a base original, além da informação de region, mais países, e as features infant_mortality e fertility. Contudo os valores das variáveis: population, gdp e life_expectancy, infos que também constam na base orginal do gapminder, possuem valores razoavelmente distintos (da base original, e um pouco distoantes dentro do próprio histórico).
No mais, após o merge com as bases originais, o infant_mortality particularmente apresenta muitos NAs, bem como o gdp.

dslabs <- dslabs::gapminder %>% 
  #anos da base gapminder::gapminder
  filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 
                     1982, 1987, 1992, 1997, 2002, 2007)) %>%  
  select(-life_expectancy, -population, -gdp) %>% 
  glimpse()
## Rows: 1,850
## Columns: 6
## $ country          <fct> "Albania", "Algeria", "Angola", "Antigua and Barbuda"…
## $ year             <int> 1962, 1962, 1962, 1962, 1962, 1962, 1962, 1962, 1962,…
## $ infant_mortality <dbl> 106.50, 148.20, NA, NA, 59.59, NA, NA, 19.50, 32.90, …
## $ fertility        <dbl> 5.96, 7.65, 7.39, 4.34, 3.09, 4.44, 4.47, 3.43, 2.80,…
## $ continent        <fct> Europe, Africa, Africa, Americas, Americas, Asia, Ame…
## $ region           <fct> Southern Europe, Northern Africa, Middle Africa, Cari…

gapminder

A base gapminder tratada tem um total de 142 países para cada um dos anos, com 12 anos distintos, entre 1952 e 2007 .

gapminder <- gapminder::gapminder %>%
  left_join(gapminder::country_codes) %>% 
  left_join(dslabs) %>% 
  mutate(region = case_when(
        country == "Afghanistan" ~ "Central Asia",
        country == "Korea, Dem. Rep." ~ "Eastern Asia",
        country == "Korea, Rep." ~ "Eastern Asia",
        country == "Myanmar" ~ "Southeast Asia",
        country == "Reunion" ~ "Eastern Africa",
        country == "Sao Tome and Principe" ~ "Central Africa",
        country == "Somalia" ~ "Eastern Africa",
        country == "Taiwan" ~ "Eastern Asia",
        country == "Turkey" ~ "Western Asia",
        country == "Yemen, Rep." ~ "Western Asia",
    TRUE ~ region)) %>% 
  janitor::clean_names() %>% 
  mutate(continent = recode(continent,
                            "Asia" = "Ásia",
                            "Europe" = "Europa", 
                            "Africa" = "África", 
                            "Americas" = "América", 
                            "Oceania" = "Oceania")) %>% 
  relocate(region, .after = continent) %>% 
  glimpse()
## Rows: 1,704
## Columns: 11
## $ country          <chr> "Afghanistan", "Afghanistan", "Afghanistan", "Afghani…
## $ continent        <fct> Ásia, Ásia, Ásia, Ásia, Ásia, Ásia, Ásia, Ásia, Ásia,…
## $ region           <chr> "Central Asia", "Central Asia", "Central Asia", "Cent…
## $ year             <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992,…
## $ life_exp         <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.85…
## $ pop              <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880…
## $ gdp_percap       <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786…
## $ iso_alpha        <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG…
## $ iso_num          <int> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8,…
## $ infant_mortality <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ fertility        <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
  #código complementar para identificar países sem região atribuída:
  #gapminder %>%   filter(is.na(region)) %>% select(country, continent, region) %>%  unique() 

gapminder_full (data frame que será utilizado)

Já a base gapminder_full, tem variedade no números de países para 58 anos distintos, entre 1950 e max(gapminder::gapminder_unfiltered$year). O ano com a maior quantidade de países é 2002, com 183 países

gapminder_full <- gapminder::gapminder_unfiltered %>% 
  filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2007)) %>%  
  left_join(gapminder::country_codes) %>% 
  left_join(dslabs) %>% 
  mutate(region = case_when(
      country == "Afghanistan" ~ "Central Asia",
      country == "Korea, Dem. Rep." ~ "Eastern Asia",
      country == "Korea, Rep." ~ "Eastern Asia",
      country == "Myanmar" ~ "Southeast Asia",
      country == "Reunion" ~ "Eastern Africa",
      country == "Sao Tome and Principe" ~ "Central Africa",
      country == "Somalia" ~ "Eastern Africa",
      country == "Taiwan" ~ "Eastern Asia",
      country == "Turkey" ~ "Western Asia",
      country == "Yemen, Rep." ~ "Western Asia",
  TRUE ~ region)) %>% 
  mutate(region = case_when(
      country == "Cyprus" ~ "Western Asia",
      country == "French Guiana" ~ "South America",
      country == "Guadeloupe" ~ "Caribbean",
      country == "Martinique" ~ "Caribbean",
      country == "Netherlands Antilles" ~ "South America",
  TRUE ~ region)) %>% 
  janitor::clean_names() %>% 
  mutate(continent = recode(continent,
                                 "Asia" = "Ásia",
                                 "Europe" = "Europa",
                                 "Africa" = "África",
                                 "Americas" = "América",
                                 "Oceania" = "Oceania")) %>%
  relocate(region, .after = continent) %>% 
  glimpse()
## Rows: 2,013
## Columns: 11
## $ country          <chr> "Afghanistan", "Afghanistan", "Afghanistan", "Afghani…
## $ continent        <fct> Ásia, Ásia, Ásia, Ásia, Ásia, Ásia, Ásia, Ásia, Ásia,…
## $ region           <chr> "Central Asia", "Central Asia", "Central Asia", "Cent…
## $ year             <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992,…
## $ life_exp         <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.85…
## $ pop              <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880…
## $ gdp_percap       <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786…
## $ iso_alpha        <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG…
## $ iso_num          <int> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8,…
## $ infant_mortality <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ fertility        <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…

view

gapminder_full %>% DT::datatable()

Magnitude

Este capítulo se baseia nas análises feitas pelo @traffordDataLab

Qtd países por continente

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2002") %>% 
  group_by(continent) %>% 
  count() %>% 
  ggplot(aes(x = continent, y = n, fill = continent), color = "white")  +
    geom_col(alpha = 0.8) +
    geom_text(aes(label = n), vjust = -0.5, size = 4, colour = "#757575") +
    geom_hline(yintercept=0, color = "lightgrey") +
    scale_fill_brewer(palette = "Set2") +
    labs(title = "",
         subtitle = "Contagem de Países por Continente, ano de 2002",
         caption = "Fonte: gapminder.org |  @traffordDataLab",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() + 
    scale_y_continuous(limits = c(0, 60)) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          #panel.grid = element_blank(),
          axis.text.y=element_blank(),
          legend.position = "none") 

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2002") %>% 
  group_by(continent) %>% 
  count() %>% 
  ggplot(aes(x = continent, y = n, fill = continent), color = "white")  +
    geom_col(alpha = 0.8) +
    geom_hline(yintercept=0, color = "lightgrey") +
    scale_fill_brewer(palette = "Set2") +
    labs(title = "",
         subtitle = "Contagem de Países por Continente, ano de 2002",
         caption = "Fonte: gapminder.org |  @traffordDataLab",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() + 
    scale_y_continuous(limits = c(0, 60)) +
    theme(panel.grid.major.x = element_blank(),
          #panel.grid.major.y = element_blank(),
          #panel.grid = element_blank(),
          #axis.text.y=element_blank(),
          legend.position = "none") 

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2002") %>% 
  group_by(continent) %>% 
  count() %>% 
  ggplot(aes(x = continent, y = n, fill = continent), color = "white")  +
    geom_col(alpha = 0.8) +
    geom_text(aes(label = n), vjust = -0.5, size = 4, colour = "#757575") +
    geom_hline(yintercept=0, color = "lightgrey") +
    scale_fill_brewer(palette = "Set2") +
    labs(title = "",
         subtitle = "Contagem de Países por Continente, ano de 2002",
         caption = "Fonte: gapminder.org |  @traffordDataLab",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() + 
    scale_y_continuous(limits = c(0, 60)) +
    theme(panel.grid.major.x = element_blank(),
          #panel.grid.major.y = element_blank(),
          #panel.grid = element_blank(),
          #axis.text.y=element_blank(),
          legend.position = "none") 

Life_exp

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2002") %>% 
  group_by(continent) %>% 
  summarise(life_exp = mean(life_exp)) %>% 
  ggplot(aes(x = continent, y = life_exp, fill = continent))  +
  geom_col(fill = "#FFD92F", alpha = 0.8) +
  geom_hline(yintercept=0, color = "lightgrey") +
  labs(title = "",
       subtitle = "Média Expectativa de Vida por Continente, ano de 2002",
       caption = "Fonte: gapminder.org |  @traffordDataLab",
       x = NULL,
       y = NULL,
       fill = NULL) +
  theme_nath() +
  theme(panel.grid.major.x = element_blank(),
        legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2002") %>% 
  group_by(continent) %>% 
  summarise(life_exp = mean(life_exp)) %>% 
  ggplot(aes(x = continent, y = life_exp, fill = continent))  +
  geom_col(alpha = 0.8) +
  geom_hline(yintercept=0, color = "lightgrey") +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "",
       subtitle = "Média Expectativa de Vida por Continente, ano de 2002",
       caption = "Fonte: gapminder.org |  @traffordDataLab",
       x = NULL,
       y = NULL,
       fill = NULL) +
  theme_nath() +
  theme(panel.grid.major.x = element_blank(),
        legend.position = "none")

Comparação

absoluta

agrupado

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952,  2002)) %>%
  group_by(year, continent) %>% 
  count() %>% 
  ggplot(aes(x = year, y = n, group = continent, fill = continent)) + 
  geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
  geom_hline(yintercept=0, color = "lightgrey") +
  scale_x_continuous(breaks = c(1952, 2002), expand = c(0, 0)) +
  scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "",
       subtitle = "Contagem de Países por Continente, comparação por período",
       caption = "Source: gapminder.org  |  @traffordDataLab",
       x = NULL,
       y = NULL,
       fill = NULL) +
  theme_nath() +
  scale_y_continuous(limits = c(0, 60)) +
  theme(panel.grid.major.x = element_blank(),
        legend.position = "bottom")

gapminder_full 
## # A tibble: 2,013 × 11
##    country   continent region  year life_exp    pop gdp_percap iso_alpha iso_num
##    <chr>     <fct>     <chr>  <int>    <dbl>  <int>      <dbl> <chr>       <int>
##  1 Afghanis… Ásia      Centr…  1952     28.8 8.43e6       779. AFG             4
##  2 Afghanis… Ásia      Centr…  1957     30.3 9.24e6       821. AFG             4
##  3 Afghanis… Ásia      Centr…  1962     32.0 1.03e7       853. AFG             4
##  4 Afghanis… Ásia      Centr…  1967     34.0 1.15e7       836. AFG             4
##  5 Afghanis… Ásia      Centr…  1972     36.1 1.31e7       740. AFG             4
##  6 Afghanis… Ásia      Centr…  1977     38.4 1.49e7       786. AFG             4
##  7 Afghanis… Ásia      Centr…  1982     39.9 1.29e7       978. AFG             4
##  8 Afghanis… Ásia      Centr…  1987     40.8 1.39e7       852. AFG             4
##  9 Afghanis… Ásia      Centr…  1992     41.7 1.63e7       649. AFG             4
## 10 Afghanis… Ásia      Centr…  1997     41.8 2.22e7       635. AFG             4
## # ℹ 2,003 more rows
## # ℹ 2 more variables: infant_mortality <dbl>, fertility <dbl>
gapminder::gapminder_unfiltered %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952,  2002)) %>%
  group_by(year, continent) %>% 
  count() %>% 
  mutate(year = as.character(year)) %>% 
  ggplot(aes(x = continent, y = n, group = year, fill = continent)) + 
  geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
  geom_hline(yintercept=0, color = "lightgrey") +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "",
       subtitle = "Contagem de Países por Continente, comparação por período: 1952 vs. 2002",
       caption = "Source: gapminder.org  |  @traffordDataLab",
       x = NULL, y = NULL, fill = NULL) +
  theme_nath() +
  scale_y_continuous(limits = c(0, 60)) +
  theme(panel.grid.major.x = element_blank(),
        legend.position = "bottom")

gapminder_full 
## # A tibble: 2,013 × 11
##    country   continent region  year life_exp    pop gdp_percap iso_alpha iso_num
##    <chr>     <fct>     <chr>  <int>    <dbl>  <int>      <dbl> <chr>       <int>
##  1 Afghanis… Ásia      Centr…  1952     28.8 8.43e6       779. AFG             4
##  2 Afghanis… Ásia      Centr…  1957     30.3 9.24e6       821. AFG             4
##  3 Afghanis… Ásia      Centr…  1962     32.0 1.03e7       853. AFG             4
##  4 Afghanis… Ásia      Centr…  1967     34.0 1.15e7       836. AFG             4
##  5 Afghanis… Ásia      Centr…  1972     36.1 1.31e7       740. AFG             4
##  6 Afghanis… Ásia      Centr…  1977     38.4 1.49e7       786. AFG             4
##  7 Afghanis… Ásia      Centr…  1982     39.9 1.29e7       978. AFG             4
##  8 Afghanis… Ásia      Centr…  1987     40.8 1.39e7       852. AFG             4
##  9 Afghanis… Ásia      Centr…  1992     41.7 1.63e7       649. AFG             4
## 10 Afghanis… Ásia      Centr…  1997     41.8 2.22e7       635. AFG             4
## # ℹ 2,003 more rows
## # ℹ 2 more variables: infant_mortality <dbl>, fertility <dbl>
gapminder::gapminder_unfiltered %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952,  2002)) %>%
  group_by(year, continent) %>% 
  count() %>% 
  mutate(year = as.character(year)) %>% 
  ggplot(aes(x = continent, y = n, group = year, 
             fill = interaction(year, continent))) + 
  geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
  geom_hline(yintercept=0, color = "lightgrey") +
  scale_fill_manual(values = c("#66C2A5", "#388C72", "#FC8D62", "#FA4F0A", 
                                  "#8DA0CB","#4964A1", "#E78AC3","#D42C94", 
                                  "#A6D854", "#77A927")) +
  labs(title = "",
       subtitle = "Contagem de Países por Continente, comparação por período: 1952 vs. 2002",
       caption = "Source: gapminder.org  |  @traffordDataLab",
       x = NULL, y = NULL, fill = NULL) +
  theme_nath() +
  scale_y_continuous(limits = c(0, 60)) +
  theme(panel.grid.major.x = element_blank(),
        legend.position = "none")

(p1 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002)) %>%
  #filter(year %in% c(1952,  1977, 2002)) %>%
  group_by(year, continent) %>% 
  count() %>% 
  ggplot(aes(x = year, y = n, group = continent, fill = continent)) + 
  geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
  geom_hline(yintercept=0, color = "lightgrey") +
  #scale_x_continuous(breaks = c(1952,  1977, 2002), expand = c(0, 0)) +
  scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "",
       subtitle = "Contagem de Países por Continente, comparação por período",
       caption = "Source: gapminder.org  |  @traffordDataLab",
       x = NULL,
       y = NULL,
       fill = NULL) +
  theme_nath() +
  scale_y_continuous(limits = c(0, 60)) +
  theme(panel.grid.major.x = element_blank(),
        legend.position = "bottom"))

agrupado + continent by years

(p0 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002)) %>%
  #filter(year %in% c(1952,  1977, 2002)) %>%
  group_by(year, continent) %>% 
  count() %>% 
  ggplot(aes(x = year, y = n, group = continent, fill = continent)) + 
  geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
   facet_wrap(. ~ continent) +
  geom_hline(yintercept=0, color = "lightgrey") +
  scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "",
       subtitle = "Contagem de Países por Continente, comparação por período",
       caption = "Source: gapminder.org  |  @traffordDataLab",
       x = NULL,
       y = NULL,
       fill = NULL) +
  theme_nath() +
  scale_y_continuous(limits = c(0, 60)) +
  theme(panel.grid.major.x = element_blank(),
        legend.position = "none"))

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002)) %>%
  #filter(year %in% c(1952,  1977, 2002)) %>%
  group_by(year, continent) %>% 
  count() %>% 
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = continent, y = n, group = year, fill = year)) + 
  geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
  #facet_grid(. ~ year, scales = "free_x") +
  scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "",
       subtitle = "Contagem de Países por Continente, de 1952 a 2002",
       caption = "Source: Gapminder.org  |  @traffordDataLab",
       x = NULL,
       y = NULL,
       fill = NULL) +
  theme_nath() +
  theme(panel.grid.major.x = element_blank(),
        legend.position = "bottom")

absoluta e relativa não normalizada

(p2 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002)) %>%
  #filter(year %in% c(1952,  1977, 2002)) %>%
  group_by(year, continent) %>% 
  count() %>% 
  ggplot(aes(x = year, y = n, fill = continent)) + 
  geom_col(colour = "white", size = 0.2, alpha = 0.8) +
  geom_hline(yintercept=0, color = "lightgrey") +
  scale_x_continuous(breaks = c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002),
                     expand = c(0, 0)) +
  scale_fill_brewer(palette = "Set2") +
  guides(fill = guide_legend(reverse = F)) +
  labs(title = "",
       subtitle = "Proporção da contagem de países por continente, comparação por período",
       caption = "Source: gapminder.org  |  @traffordDataLab",
       x = NULL,
       y = NULL,
       fill = NULL) +
  theme_nath() +
  theme(panel.grid.major.x = element_blank(),
        legend.position = "right"))

relativa

(p3 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002)) %>%
  #filter(year %in% c(1952,  1977, 2002)) %>%
  group_by(year, continent) %>% 
  count() %>% 
  ggplot(aes(x = year, y = n, fill = continent)) + 
  geom_col(position = "fill", colour = "white", size = 0.2, alpha = 0.8) +
  scale_x_continuous(breaks = c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002), expand = c(0, 0)) +
  scale_y_continuous(labels = scales::percent, expand = c(0, 0)) +
  scale_fill_brewer(palette = "Set2") +
  guides(fill = guide_legend(reverse = F)) +
  labs(title = "",
       subtitle = "Proporção da contagem de países por continente, comparação por período",
       caption = "Source: gapminder.org  |  @traffordDataLab",
       x = NULL,
       y = NULL,
       fill = NULL) +
  theme_nath() +
 geom_hline(yintercept=0, color = "lightgrey") +
  theme(panel.grid.major.x = element_blank(),
        legend.position = "right"))

clean versions

#patchwork::

(p11 <- p1 +
  theme(legend.position = "none") +
  labs(subtitle = "",
       caption = ""))
(p22 <- p2 +
  theme(legend.position = "none") +
  labs(subtitle = "",
       caption = ""))
(p33 <- p3 +
  theme(legend.position = "none") +
  labs(subtitle = "",
       caption = "") )
  

(p11 / p22 / p33)

Relação

fertility x life_exp (2007)

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c("2007")) %>%
  #filter(continent == "Europa") %>%
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = fertility, y = life_exp, color = year, label = country))  +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    geom_point(shape=16, size  = 2, stroke = 0, alpha = 0.6) +
    viridis::scale_color_viridis(discrete = T, direction = 1) +
    labs(title = "",
         #subtitle = "Expectativa de Vida vs. Fertilidade, comparação por período",
         subtitle = "Expectativa de Vida vs. Fertilidade",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(1, 8), minor_breaks = seq(1, 8, 1), n.breaks = 8) +
    scale_y_continuous(limits = c(30, 87)) +
    theme(#panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "right"))

plotly::ggplotly(p)
temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c("2007")) %>%
  #filter(continent == "Europa") %>%
  mutate(year = as_factor(year)) 
  
(p <- temp %>% 
  ggplot(aes(x = fertility, y = life_exp, color = year, label = country))  +
    geom_point(shape=16, size  = 2, stroke = 0, alpha = 0.1) +
    geom_point(data = . %>% filter(country == "Japan"), 
               shape=16, size  = 2, stroke = 0, color = "#21918c") +
    ggrepel::geom_label_repel(data = . %>% filter(country == "Japan"), 
                          color = "#21918c",
                          max.overlaps = 50, 
                          nudge_x = -.3, nudge_y = 3,
                          segment.curvature = 0.5) +
    scale_color_manual(values = c("#440154")) +
    labs(title = "",
         subtitle = "Expectativa de Vida vs. Fertilidade",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(1, 8), minor_breaks = seq(1, 8, 1), n.breaks = 8) +
    scale_y_continuous(limits = c(30, 87)) +
    theme(#panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "right"))

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c("2007")) %>%
  mutate(year = as_factor(year)) 
  
(p <- temp %>% 
  ggplot(aes(x = fertility, y = life_exp, color = year, label = country))  +
    geom_point(shape=16, size  = 2, stroke = 0, alpha = 0.1) +
    geom_point(data = . %>% filter(country == "Japan"), 
               shape=16, size  = 2, stroke = 0, color = "#21918c") +
    ggrepel::geom_label_repel(data = . %>% filter(country == "Japan"), 
                          color = "#21918c",
                          max.overlaps = 50, 
                          nudge_x = -.3, nudge_y = 3,
                          segment.curvature = 0.5) +
    geom_point(data = . %>% filter(country == "China"), 
               shape=16, size  = 2, stroke = 0, color = "#21918c") +
    ggrepel::geom_label_repel(data = . %>% filter(country == "China"), 
                              color = "#21918c",
                              max.overlaps = 50, 
                              nudge_x = -.1, nudge_y = -1.5,
                              box.padding = 1,
                              segment.curvature = 0.5) +
    scale_color_manual(values = c("#440154")) +
    labs(title = "",
         subtitle = "Expectativa de Vida vs. Fertilidade",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(1, 8), minor_breaks = seq(1, 8, 1), n.breaks = 8) +
    scale_y_continuous(limits = c(30, 87)) +
    theme(#panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "right"))

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c("2007")) %>%
  #filter(continent == "Europa") %>%
  mutate(year = as_factor(year)) 
  
(p <- temp %>% 
  ggplot(aes(x = fertility, y = life_exp, color = year, label = country))  +
    geom_point(shape=16, size  = 2, stroke = 0, alpha = 0.1) +
    # geom_point(data = . %>% filter(life_exp == max(life_exp)), 
    #            color = "#21918c") +
    # ggrepel::geom_label_repel(data = . %>% 
    #                             filter(life_exp >70, fertility >3, fertility <4), 
    #                   color = "#21918c",
    #                   max.overlaps = 50, 
    #                   nudge_x = -.3, nudge_y = 3,
    #                   segment.curvature = 0.9) +
    geom_point(data = . %>% filter(country == "Syria"),
               shape=16, size  = 2, stroke = 0, color = "#21918c") +
    ggrepel::geom_label_repel(data = . %>% filter(country == "Syria"), 
                          color = "#21918c",
                          max.overlaps = 50, 
                          nudge_x = .3, nudge_y = 3,
                          box.padding = 1,
                          segment.curvature = 0.9) +
    geom_point(data = . %>% filter(country == "Japan"), 
               shape=16, size  = 2, stroke = 0, color = "#21918c") +
    ggrepel::geom_label_repel(data = . %>% filter(country == "Japan"), 
                          color = "#21918c",
                          max.overlaps = 50, 
                          nudge_x = -.3, nudge_y = 3,
                          segment.curvature = 0.5) +
    geom_point(data = . %>% filter(country == "China"), 
               shape=16, size  = 2, stroke = 0, color = "#21918c") +
    ggrepel::geom_label_repel(data = . %>% filter(country == "China"), 
                              color = "#21918c",
                              max.overlaps = 50, 
                              nudge_x = -.1, nudge_y = -1.5,
                              box.padding = 1,
                              segment.curvature = 0.5) +
    scale_color_manual(values = c("#440154")) +
    labs(title = "",
         subtitle = "Expectativa de Vida vs. Fertilidade",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(1, 8), minor_breaks = seq(1, 8, 1), n.breaks = 8) +
    scale_y_continuous(limits = c(30, 87)) +
    theme(#panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "right"))

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c("2007")) %>%
  #filter(continent == "Europa") %>%
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = fertility, y = life_exp, color = year, label = country))  +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    geom_point(shape=16, size  = 2, stroke = 0, alpha = 0.6) +
    geom_smooth(method = "lm") +
    viridis::scale_color_viridis(discrete = T, direction = 1) +
    labs(title = "",
         #subtitle = "Expectativa de Vida vs. Fertilidade, comparação por período",
         subtitle = "Expectativa de Vida vs. Fertilidade",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(1, 8), minor_breaks = seq(1, 8, 1), n.breaks = 8) +
    scale_y_continuous(limits = c(30, 87)) +
    theme(#panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "right"))

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c("2007")) %>%
  #filter(continent == "Europa") %>%
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = fertility, y = life_exp, color = year, label = country))  +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    geom_point(shape=16, size  = 2, stroke = 0, alpha = 0.6) +
    geom_smooth() +
    viridis::scale_color_viridis(discrete = T, direction = 1) +
    #scale_color_manual(values = c('#9ccb86','#cf597e', '#009392','#eeb479' )) +
    labs(title = "",
         #subtitle = "Expectativa de Vida vs. Fertilidade, comparação por período",
         subtitle = "Expectativa de Vida vs. Fertilidade",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(1, 8), minor_breaks = seq(1, 8, 1), n.breaks = 8) +
    scale_y_continuous(limits = c(30, 87)) +
    theme(#panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "right"))

overplotting

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c("2007")) %>%
  #filter(continent == "Europa") %>%
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = fertility, y = life_exp, label = country))  +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    geom_point(shape=16, size  = 2, stroke = 0, alpha = 0.6, color = "#440154") +
    #viridis::scale_color_viridis(discrete = T, direction = 1) +
    labs(title = "",
         #subtitle = "Expectativa de Vida vs. Fertilidade, comparação por período",
         subtitle = "Expectativa de Vida vs. Fertilidade, 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(1, 8), minor_breaks = seq(1, 8, 1), n.breaks = 8) +
    scale_y_continuous(limits = c(30, 87)) +
    theme(#panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "right"))

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  #filter(year %in% c("2007")) %>%
  #filter(continent == "Europa") %>%
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = fertility, y = life_exp, label = country))  +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    geom_point(shape=16, size  = 2, stroke = 0, alpha = 0.6, color = "#440154") +
    #viridis::scale_color_viridis(discrete = T, direction = 1) +
    labs(title = "",
         #subtitle = "Expectativa de Vida vs. Fertilidade, comparação por período",
         subtitle = "Expectativa de Vida vs. Fertilidade, de 1952 a 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(1, 8), minor_breaks = seq(1, 8, 1), n.breaks = 8) +
    scale_y_continuous(limits = c(30, 87)) +
    theme(#panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "right"))

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  #filter(year %in% c("2007")) %>%
  #filter(continent == "Europa") %>%
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = fertility, y = life_exp, label = country))  +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    geom_point(shape=16, size  = 2, stroke = 0, alpha = 0.4, color = "#440154") +
    #viridis::scale_color_viridis(discrete = T, direction = 1) +
    labs(title = "",
         #subtitle = "Expectativa de Vida vs. Fertilidade, comparação por período",
         subtitle = "Expectativa de Vida vs. Fertilidade, de 1952 a 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(1, 8), minor_breaks = seq(1, 8, 1), n.breaks = 8) +
    scale_y_continuous(limits = c(30, 87)) +
    theme(#panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "right"))

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  #filter(year %in% c("2007")) %>%
  #filter(continent == "Europa") %>%
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = fertility, y = life_exp, label = country))  +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    geom_point(shape=16, size  = 2, stroke = 0, alpha = 0.3, color = "#440154") +
    #viridis::scale_color_viridis(discrete = T, direction = 1) +
    labs(title = "",
         #subtitle = "Expectativa de Vida vs. Fertilidade, comparação por período",
         subtitle = "Expectativa de Vida vs. Fertilidade, de 1952 a 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(1, 8), minor_breaks = seq(1, 8, 1), n.breaks = 8) +
    scale_y_continuous(limits = c(30, 87)) +
    theme(#panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "right"))

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  #filter(year %in% c("2007")) %>%
  #filter(continent == "Europa") %>%
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = fertility, y = life_exp, label = country))  +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    geom_point(shape=16, size  = 2, stroke = 0, alpha = 1, color = "#440154") +
    #viridis::scale_color_viridis(discrete = T, direction = 1) +
    labs(title = "",
         #subtitle = "Expectativa de Vida vs. Fertilidade, comparação por período",
         subtitle = "Expectativa de Vida vs. Fertilidade, de 1952 a 2007",
        # caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(1, 8), minor_breaks = seq(1, 8, 1), n.breaks = 8) +
    scale_y_continuous(limits = c(30, 87)) +
    theme(#panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "right"))

colorBlindness::cvdPlot(p)

continents

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  #filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002)) %>%
  filter(year == "2007") %>%
  #filter(continent == "Oceania") %>%
  group_by(year, country, continent) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm=T),
    fertility = mean(fertility, na.rm=T)
  ) %>% 
  ungroup() %>% 
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = fertility, y = life_exp, color = continent))  +
    #ggplot(aes(x = continent, y = n, group = year, fill = year)) + 
    #geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    #geom_text(aes(label = country), vjust = "inward") +
    geom_point() +
    geom_smooth(method = "glm") +
    facet_grid(. ~ continent, scales = "free_x") +
    scale_color_brewer(palette = "Set2") +
    # labs(title = "",
    #      subtitle = "Contagem de Países por Continente, de 1952 a 2002",
    #      caption = "Source: Gapminder.org",
    #      x = NULL,
    #      y = NULL,
    #      fill = NULL) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none"))

# plotly::ggplotly(p)

gdp_percap x life_exp

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002)) %>%
  filter(year == "2002") %>%
  #filter(continent == "Oceania") %>%
  # group_by(year, country, continent) %>% 
  # summarise(
  #   life_exp = mean(life_exp, na.rm=T),
  #   fertility = mean(fertility, na.rm=T)
  # ) %>% 
  # ungroup() %>% 
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = gdp_percap, y = life_exp, color = continent))  +
    #ggplot(aes(x = continent, y = n, group = year, fill = year)) + 
    #geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    #stat_summary(geom="point", fun.data = ~mean(.x,na.rm=T)) +
    #geom_text(aes(label = country), vjust = "inward") +
    geom_point() +
    #geom_smooth(method = "glm") +
    scale_color_brewer(palette = "Set2") +
    # labs(title = "",
    #      subtitle = "Contagem de Países por Continente, de 1952 a 2002",
    #      caption = "Source: Gapminder.org",
    #      x = NULL,
    #      y = NULL,
    #      fill = NULL) +
    #scale_x_log10(labels = scales::dollar) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none"))

# plotly::ggplotly(p)
( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002)) %>%
  filter(year == "2002") %>%
  #filter(continent == "Oceania") %>%
  # group_by(year, country, continent) %>% 
  # summarise(
  #   life_exp = mean(life_exp, na.rm=T),
  #   fertility = mean(fertility, na.rm=T)
  # ) %>% 
  # ungroup() %>% 
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = gdp_percap, y = life_exp, color = continent))  +
    #ggplot(aes(x = continent, y = n, group = year, fill = year)) + 
    #geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    #stat_summary(geom="point", fun.data = ~mean(.x,na.rm=T)) +
    #geom_text(aes(label = country), vjust = "inward") +
    geom_point(aes(size = pop)) +
    #geom_smooth(method = "glm") +
    scale_color_brewer(palette = "Set2") +
    # labs(title = "",
    #      subtitle = "Contagem de Países por Continente, de 1952 a 2002",
    #      caption = "Source: Gapminder.org",
    #      x = NULL,
    #      y = NULL,
    #      fill = NULL) +
    #scale_x_log10(labels = scales::dollar) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none"))

# plotly::ggplotly(p)
( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002)) %>%
  filter(year == "2002") %>%
  #filter(continent == "Oceania") %>%
  # group_by(year, country, continent) %>% 
  # summarise(
  #   life_exp = mean(life_exp, na.rm=T),
  #   fertility = mean(fertility, na.rm=T)
  # ) %>% 
  # ungroup() %>% 
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = gdp_percap, y = life_exp, color = continent))  +
    #ggplot(aes(x = continent, y = n, group = year, fill = year)) + 
    #geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    #stat_summary(geom="point", fun.data = ~mean(.x,na.rm=T)) +
    #geom_text(aes(label = country), vjust = "inward") +
    geom_point() +
    #geom_smooth(method = "glm") +
    facet_grid(. ~ continent, scales = "free_x") +
    scale_color_brewer(palette = "Set2") +
    # labs(title = "",
    #      subtitle = "Contagem de Países por Continente, de 1952 a 2002",
    #      caption = "Source: Gapminder.org",
    #      x = NULL,
    #      y = NULL,
    #      fill = NULL) +
    #scale_x_log10(labels = scales::dollar) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none"))

# plotly::ggplotly(p)
( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  #filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002)) %>%
  filter(year == "2007") %>%
  filter(continent == "Europa") %>%
  # group_by(year, country, continent) %>% 
  # summarise(
  #   life_exp = mean(life_exp, na.rm=T),
  #   fertility = mean(fertility, na.rm=T)
  # ) %>% 
  # ungroup() %>% 
  mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = gdp_percap, y = life_exp, color = continent))  +
    #ggplot(aes(x = continent, y = n, group = year, fill = year)) + 
    #geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    #stat_summary(geom="point", fun.data = ~mean(.x,na.rm=T)) +
    #geom_text(aes(label = country), vjust = "inward") +
    geom_point() +
    #geom_smooth(method = "glm") +
    facet_grid(. ~ continent, scales = "free_x") +
    scale_color_brewer(palette = "Set2") +
    # labs(title = "",
    #      subtitle = "Contagem de Países por Continente, de 1952 a 2002",
    #      caption = "Source: Gapminder.org",
    #      x = NULL,
    #      y = NULL,
    #      fill = NULL) +
    scale_x_continuous(labels = scales::dollar) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none"))

# plotly::ggplotly(p)

Evolução

fertility

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  #filter(year == "2007") %>% 
  group_by(continent, year) %>% 
  summarise(
    NA_fertility = sum(is.na(fertility)),
    mean_fertility = mean(fertility, na.rm = T),
    ) %>% 
  ggplot(aes(x = year, y = mean_fertility, color = continent))  +
    #geom_point(color = "gray") +
    #geom_line(aes(group = country), color = "gray") +
    # geom_point(alpha = 0.3) +
    geom_line() +
    ggrepel::geom_label_repel(data = . %>% filter(year == 1962), aes(label = continent)) +
    scale_colour_brewer(palette = "Set2") +
    guides(fill = guide_legend(reverse = F)) +
    labs(title = "",
       subtitle = "...",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
          legend.position = "none")

life_exp

line

continent

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  # mutate(year = lubridate::year(lubridate::as_date(year))) %>% 
  mutate(year = as.integer(year)) %>% 
  ggplot(aes(x = year, y = life_exp))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(color = "#440154", size = 0.8) +
    #geom_point(color = "#440154") +
    #ggrepel::geom_label_repel(data = . %>% filter(year == 1962), aes(label = continent)) +
    labs(title = "",
         subtitle = "Evolução da expectativa de vida, média dos países",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    #scale_x_date(date_breaks = "1 year") +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          #panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  # mutate(year = lubridate::year(lubridate::as_date(year))) %>% 
  mutate(year = as.integer(year)) %>% 
  ggplot(aes(x = year, y = life_exp))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(color = "#440154", size = 0.8) +
    geom_point(color = "#440154") +
    #ggrepel::geom_label_repel(data = . %>% filter(year == 1962), aes(label = continent)) +
    labs(title = "",
          subtitle = "Evolução da expectativa de vida, média dos países",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    #scale_x_date(date_breaks = "1 year") +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          #panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  # mutate(year = lubridate::year(lubridate::as_date(year))) %>% 
  mutate(year = as.integer(year)) %>% 
  ggplot(aes(x = year, y = life_exp))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    #geom_line(color = "#440154", size = 0.8) +
    geom_point(color = "#440154") +
    #ggrepel::geom_label_repel(data = . %>% filter(year == 1962), aes(label = continent)) +
    labs(title = "",
          subtitle = "Evolução da expectativa de vida, média dos países",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    #scale_x_date(date_breaks = "1 year") +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          #panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  mutate(year = as.integer(year)) %>% 
  ggplot(aes(x = year, y = life_exp))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(color = "#440154", size = 0.8) +
    #geom_point(color = "#440154") +
    ggrepel::geom_text_repel(data = . %>% filter(year == min(year)), 
                             aes(label = round(life_exp,0)),
                             color = "#440154", nudge_x = -1) +
    ggrepel::geom_text_repel(data = . %>% filter(year == max(year)), 
                             aes(label = round(life_exp,0)),
                             color = "#440154", nudge_x = 1) +
    labs(title = "",
          subtitle = "Evolução da expectativa de vida, média dos países",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  mutate(year = as.integer(year)) %>% 
  ggplot(aes(x = year, y = life_exp))  +
    geom_vline(xintercept=1987, color = "lightgrey", linetype =2) +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(color = "#440154", size = 0.8) +
    #geom_point(color = "#440154") +
    ggrepel::geom_text_repel(data = . %>% filter(year == min(year)), 
                             aes(label = round(life_exp,0)),
                             color = "#440154", nudge_x = -1) +
    ggrepel::geom_text_repel(data = . %>% filter(year == max(year)), 
                             aes(label = round(life_exp,0)),
                             color = "#440154", nudge_x = 1) +
    ggrepel::geom_text_repel(data = . %>% filter(year == 1987), 
                             aes(label = round(life_exp,0)),
                             color = "#440154", nudge_y = 1) +
    labs(title = "",
           subtitle = "Evolução da expectativa de vida, média dos países",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          legend.position = "none")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(continent, year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  mutate(year = as.integer(year)) 

temp %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(size = 0.8) +
    labs(title = "",
          subtitle = "Evolução da expectativa de vida, média dos continentes",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_color_brewer(palette = "Set2") +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          legend.position = "right")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(continent, year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  mutate(year = as.integer(year)) 

temp_new <-  temp %>%
    filter(year == max(year)) %>%
    distinct(continent, life_exp)

legend_ord <- rev(levels(with(temp_new, reorder(continent, life_exp))))

temp %>% 
  mutate(continent = as_factor(continent)) %>% 
  mutate(continent = forcats::fct_relevel(continent, 
                      c( "FSU", "Europa", "América", "Oceania","Ásia","África"))) %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(size = 0.8) +
    labs(title = "",
           subtitle = "Evolução da expectativa de vida, média dos continentes",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_color_manual(
      values = c("#E78AC3","#FC8D62",  "#A6D854", "#8DA0CB","#66C2A5")) +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          legend.position = "right")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(continent, year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  mutate(year = as.integer(year)) 

temp_new <-  temp %>%
    filter(year == max(year)) %>% 
    distinct(continent, life_exp)

temp %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(size = 0.8) +
    geom_point() +
    labs(title = "",
           subtitle = "Evolução da Expectativa de Vida, média de todos os países",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_color_brewer(palette = "Set2") +
    scale_y_continuous(limits = c(40, 80), 
                       sec.axis = dup_axis(
                         breaks = temp_new$life_exp,
                         labels = temp_new$continent,
                         name = NULL)
                       ) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          legend.position = "none")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(continent, year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  mutate(year = as.integer(year)) 

temp %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(size = 0.8) +
    ggrepel::geom_text_repel(data = . %>% filter(year == 1962), aes(label = continent),
                             vjust = 1, hjust = 0.1) +
    labs(title = "",
           subtitle = "Evolução da Expectativa de Vida, por continente",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_color_brewer(palette = "Set2") +
    scale_y_continuous(limits = c(40, 80), guide = guide_axis(position = 'right')) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          legend.position = "none")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(continent, year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  mutate(year = as.integer(year)) 

temp %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(size = 0.8) +
    ggrepel::geom_label_repel(data = . %>% filter(year == 1992), aes(label = continent)) +
    # ggrepel::geom_text_repel(data = . %>% filter(year == max(year)), 
    #                          aes(label = round(life_exp,0)),nudge_x = 1) +
    # ggrepel::geom_text_repel(data = . %>% filter(year == min(year)), 
    #                          aes(label = round(life_exp,0)), nudge_x = -1) +
    labs(title = "",
          subtitle = "Evolução da expectativa de vida, média dos continentes",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_color_brewer(palette = "Set2") +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          #axis.text.y=element_blank(),
          legend.position = "none")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(continent, year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  mutate(year = as.integer(year)) 

temp %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(size = 0.8) +
    ggrepel::geom_label_repel(data = . %>% filter(year == 1992), aes(label = continent)) +
    # ggrepel::geom_text_repel(data = . %>% filter(year == max(year)), 
    #                          aes(label = round(life_exp,0)),nudge_x = 1) +
    # ggrepel::geom_text_repel(data = . %>% filter(year == min(year)), 
    #                          aes(label = round(life_exp,0)), nudge_x = -1) +
    labs(title = "",
          subtitle = "Evolução da expectativa de vida, média dos continentes",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_color_brewer(palette = "Set2") +
    scale_y_continuous(limits = c(40, 80), guide = guide_axis(position = 'right')) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          #axis.text.y=element_blank(),
          legend.position = "none")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(continent, year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  mutate(year = as.integer(year)) 

temp %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(size = 0.8) +
    ggrepel::geom_label_repel(data = . %>% filter(year == 1992), aes(label = continent)) +
    ggrepel::geom_text_repel(data = . %>% filter(year == max(year)), 
                             aes(label = round(life_exp,0)),nudge_x = 1) +
    ggrepel::geom_text_repel(data = . %>% filter(year == min(year)), 
                             aes(label = round(life_exp,0)), nudge_x = -1) +
    labs(title = "",
           subtitle = "Evolução da expectativa de vida, média dos continentes",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_color_brewer(palette = "Set2") +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          axis.text.y=element_blank(),
          legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(continent, year) %>% 
  summarise(
    NA_fertility = sum(is.na(life_exp)),
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    #geom_point(color = "gray") +
    #geom_line(aes(group = country), color = "gray") +
    # geom_point(alpha = 0.3) +
    geom_line(size = 0.8) +
    ggrepel::geom_label_repel(data = . %>% filter(year == 1962), aes(label = continent)) +
    scale_colour_brewer(palette = "Set2") +
    labs(title = "",
       subtitle = "...",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80), guide = guide_axis(position = 'right')) +
    theme(panel.grid.major.x = element_blank(),
          legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  ggplot(aes(x = year, y = life_exp))  +
    scale_color_manual(values = c("#440154")) +
    geom_point(alpha = 0.3, color="lightgrey") +
    geom_line(aes(group = country), alpha = 0.3, color="lightgrey") +
    geom_smooth(color = "#440154") + 
    labs(title = "",
       subtitle = "Evolução da expectativa de vida, média dos países",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          legend.position = "none")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year >= 1962, year <= 2007) %>% 
  group_by(year) %>% 
  summarise(
    life_exp = mean(life_exp, na.rm = T),
    ) %>% 
  # mutate(year = lubridate::year(lubridate::as_date(year))) %>% 
  mutate(year = as.integer(year))

temp2 <- tibble(
  year = c(2007, 2008, 2009, 2010),
  life_exp = c(67.8, 68, 68.2, 68.6),
  upper_bound = c(68.1,68.5,69.2,70),
  lower_bound = c(67.5,67.5,67,67.1))
    
temp %>% 
  ggplot(aes(x = year, y = life_exp))  +
    geom_hline(yintercept=40, color = "lightgrey") +
    geom_line(color = "#440154", size = 0.8) +
    #geom_point(color = "#440154") +
    #ggrepel::geom_label_repel(data = . %>% filter(year == 1962), aes(label = continent)) +
    geom_smooth(data=temp2, aes(x = year, y = life_exp, ymax=upper_bound, ymin=lower_bound), 
              colour='#cf4446',  stat='identity', linetype = "twodash") +
    labs(title = "",
       subtitle = "Evolução da expectativa de vida, média dos países",
           caption = "Source: Gapminder.org",
           x = NULL,
           y = NULL,
           fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    #scale_x_date(date_breaks = "1 year") +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          #panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          legend.position = "none")

facet/ grouping
temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  # filter(continent %in% c("América")) %>%
  filter(year >= 1962, year <= 2007) 

temp %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_point(alpha = 0.3, color="lightgrey", size = .5) +
    geom_line(aes(group = country), alpha = 0.3, color="lightgrey") +
    geom_smooth() + 
  scale_colour_brewer(palette = "Set2") +
  facet_wrap(continent ~ .) +
    labs(title = "",
       subtitle = "Evolução da expectativa de vida, média dos continentes",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1965, 2010), n.breaks = 4) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          legend.position = "none")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  # filter(continent %in% c("América")) %>%
  filter(year >= 1962, year <= 2007) 

temp %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_point(alpha = 0.3, color="lightgrey", size = .5) +
    geom_line(aes(group = country), alpha = 0.3, color="lightgrey") +
    geom_smooth() + 
  scale_colour_brewer(palette = "Set2") +
  #facet_wrap(continent ~ .) +
    labs(title = "",
       subtitle = "Evolução da expectativa de vida, média dos continentes",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1965, 2010), n.breaks = 4) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          legend.position = "none")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  # filter(continent %in% c("América")) %>%
  filter(year >= 1962, year <= 2007) 

temp %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_point(size = .5, alpha = 0.6) +
    geom_line(aes(group = country), alpha = 0.6) +
    #geom_smooth() + 
  scale_colour_brewer(palette = "Set2") +
  facet_wrap(continent ~ .) +
    labs(title = "",
       subtitle = "Evolução da expectativa de vida, média dos continentes",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1965, 2010), n.breaks = 4) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          legend.position = "none")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  # filter(continent %in% c("América")) %>%
  filter(year >= 1962, year <= 2007) 

temp %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_point(alpha = 0.6, size = .5) +
    geom_line(aes(group = country), alpha = 0.6) +
    #geom_smooth() + 
  scale_colour_brewer(palette = "Set2") +
  #facet_wrap(continent ~ .) +
    labs(title = "",
       subtitle = "Evolução da expectativa de vida, média dos continentes",
       #caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1965, 2010), n.breaks = 4) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          legend.position = "none")

temp <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(continent %in% c("América")) %>%
  filter(year >= 1962, year <= 2007) 

temp %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
   scale_color_manual(values = c("#FC8D62")) +
    geom_point(alpha = 0.3, color="lightgrey") +
    geom_line(aes(group = country), alpha = 0.3, color="lightgrey") +
    geom_smooth() + 
    labs(title = "",
       subtitle = "Evolução da expectativa de vida, média do continente Americano",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    scale_y_continuous(limits = c(40, 80)) +
    scale_x_continuous(limits = c(1960, 2010), n.breaks = 10) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          legend.position = "none")

line + variability (by countries)

gapminder %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    #geom_point(color = "gray") +
    #geom_line(aes(group = country), color = "gray") +
    geom_point(alpha = 0.3) +
    geom_line(aes(group = country), alpha = 0.3) +
    scale_colour_brewer(palette = "Set2") +
    guides(fill = guide_legend(reverse = F)) +
    labs(title = "",
       subtitle = "...",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank())

gapminder %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_point(alpha = 0.3, color = "lightgray") +
    geom_line(aes(group = country), alpha = 0.3, color = "lightgray") +
    geom_smooth() +
    scale_colour_brewer(palette = "Set2") +
    guides(fill = guide_legend(reverse = F)) +
    labs(title = "",
       subtitle = "...",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank())

gapminder %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    #geom_point(color = "gray") +
    #geom_line(aes(group = country), color = "gray") +
    geom_point(alpha = 0.3) +
    geom_line(aes(group = country), alpha = 0.3) +
    geom_smooth() +
    scale_colour_brewer(palette = "Set2") +
    guides(fill = guide_legend(reverse = F)) +
    labs(title = "",
       subtitle = "...",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank())

gapminder %>% 
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_point(alpha = 0.3, color="lightgrey") +
    geom_line(aes(group = country), alpha = 0.3, color="lightgrey") +
    geom_smooth() + 
    facet_grid( continent ~ .) +
    scale_colour_brewer(palette = "Set2") +
    labs(title = "",
       subtitle = "Evolução da Expectativa de Vida Média, por continente",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
          legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002)) %>%
  ggplot(aes(x = year, y = life_exp, color = continent))  +
    geom_point(alpha = 0.3) +
    geom_line(aes(group = country), alpha = 0.3) +
    geom_smooth() + 
    facet_grid( continent ~ .) +
    scale_colour_brewer(palette = "Set2") +
    labs(title = "",
       subtitle = "...",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
          legend.position = "none")

dot - 1962 vs. 2007

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(continent %in% c("América", "Ásia", "Europa")) %>% 
  filter(year %in% c("1962", "2007")) %>% 
  group_by(year, continent, region) %>%
  summarise(
    life_exp = mean(life_exp, na.rm=T)
  ) %>% 
  ungroup() %>%
  mutate(year = as_factor(year)) %>% 
  mutate(color = paste(continent,"-", region,"-", year)) %>% 
  ggplot(aes(x = life_exp, y = region, color = color, label = year))  +
    geom_line(aes(group = region), color = "lightgrey", size = 1.8, alpha = 0.3) +
    geom_point() +
    scale_color_manual(values = 
                         c(rep(c("lightgrey", "#FC8D62"),4), 
                           rep(c("lightgrey", "#8DA0CB"),6), 
                           rep(c("lightgrey", "#E78AC3"),5))) +
#Set2 = c("#66C2A5", "#FC8D62", "#8DA0CB", "#E78AC3", "#A6D854", "#FFD92F", "#E5C494", "#B3B3B3")
    #scale_colour_brewer(palette = "Set2") +
    facet_grid(continent ~ ., scales = "free", space = "free_y") +
    labs(title = "",
       subtitle = "...",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = .05),
          #panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = .05),
          #strip.text.y.left = element_text(angle = 0),
          strip.text.y = element_text(angle = 0),
          legend.position = "none")) 

( p <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(continent %in% c("América", "Ásia", "Europa")) %>% 
  filter(year %in% c("1962", "2007")) %>% 
  group_by(year, continent, region) %>%
  summarise(
    life_exp = mean(life_exp, na.rm=T)
  ) %>% 
  ungroup() %>%
  mutate(year = as_factor(year)) %>% 
  mutate(color = paste(continent,"-", region,"-", year)) %>% 
  ggplot(aes(x = life_exp, y = region, label = year, shape = year))  +
    geom_line(aes(group = region, color = continent), size = 1.8, alpha = 0.3) +
    geom_point(data = . %>% filter(year == "2007"), aes(color = continent), size = 2) +
    geom_point(data = . %>% filter(year == "1962"), aes(color = continent), alpha = .6) +
    scale_color_manual(values = c("#FC8D62", "#8DA0CB", "#E78AC3")) +
    #scale_color_manual(values = c("#66C2A5", "#FC8D62", "#8DA0CB", "#E78AC3", "#A6D854")) +
    #scale_colour_brewer(palette = "Set2") +
    facet_grid(continent ~ ., scales = "free", space = "free_y") +
    labs(title = "",
       subtitle = "Evolução da Expectativa de Vida por subdivisões dos continentes",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme_nath() +
    guides(color=FALSE) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = .05),
          #panel.grid.minor = element_line(linetype = "dotted", colour = "lightgrey", size = .05),
          #strip.text.y.left = element_text(angle = 0),
          strip.text.y = element_text(angle = 0),
          legend.position = "top")) 

Distribuição

Países

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c("1962", "2007")) %>% 
  select(life_exp, year) %>% 
  group_by(year) %>% 
  skimr::skim()
Data summary
Name Piped data
Number of rows 325
Number of columns 2
_______________________
Column type frequency:
numeric 1
________________________
Group variables year

Variable type: numeric

skim_variable year n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
life_exp 1962 0 1 54.04 11.96 32.00 43.76 52.10 65.43 73.68 ▃▇▃▅▆
life_exp 2007 0 1 67.79 11.28 39.61 59.80 71.76 76.18 82.60 ▂▂▃▇▇
gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_histogram(bins = 11, fill = "#440154", alpha = 0.9) +  
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          # panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp, y = after_stat(count/sum(count))))  +
    geom_histogram(bins = 11, fill = "#440154", alpha = 0.9) +  
    theme_nath() +
    scale_y_continuous(labels = scales::percent) +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          # panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "none")

p1 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_histogram(bins = 11, fill = "#440154", alpha = 0.9) +  
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         x = NULL,
         y = NULL,
         fill = NULL) +
    # scale_y_continuous(limits = c(0, 50)) +
    theme(panel.grid.major.x = element_blank(),
          # panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "none")

p2 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 0.7, fill = "#440154", alpha = 0.9) +  
    theme_nath() +
    labs(title = "",
      caption = "Source: Gapminder.org",
     x = NULL,
     y = NULL,
     fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          #axis.text.y=element_blank(),
         legend.position = "none")

p1 + p2

  patchwork::plot_annotation(title = 'Distribuição da expectativa de vida dos países, ano de 2007') 
## $title
## [1] "Distribuição da expectativa de vida dos países, ano de 2007"
## 
## $subtitle
## list()
## attr(,"class")
## [1] "waiver"
## 
## $caption
## list()
## attr(,"class")
## [1] "waiver"
## 
## $tag_levels
## list()
## attr(,"class")
## [1] "waiver"
## 
## $tag_prefix
## list()
## attr(,"class")
## [1] "waiver"
## 
## $tag_suffix
## list()
## attr(,"class")
## [1] "waiver"
## 
## $tag_sep
## list()
## attr(,"class")
## [1] "waiver"
## 
## $theme
## list()
## attr(,"class")
## [1] "waiver"
## 
## attr(,"class")
## [1] "plot_annotation"
p1 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_histogram(bins = 11, fill = "#440154", alpha = 0.9) +  
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         x = NULL,
         y = NULL,
         fill = NULL) +
    # scale_y_continuous(limits = c(0, 50)) +
    theme(panel.grid.major.x = element_blank(),
          # panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
         legend.position = "none")

p2 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 0.7, color = "#21918c", fill = "#21918c", alpha = 0.9) +  
    geom_text(aes(x=50, y = 0.025, label = "Soma da área \n igual a 1"),
            linetype="dashed", color = "#21918c") +
    theme_nath() +
    labs(title = "",
      caption = "Source: Gapminder.org",
     x = NULL,
     y = NULL,
     fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          #axis.text.y=element_blank(),
         legend.position = "none")

p1 + p2

  patchwork::plot_annotation(title = 'Distribuição da expectativa de vida dos países, ano de 2007') 
## $title
## [1] "Distribuição da expectativa de vida dos países, ano de 2007"
## 
## $subtitle
## list()
## attr(,"class")
## [1] "waiver"
## 
## $caption
## list()
## attr(,"class")
## [1] "waiver"
## 
## $tag_levels
## list()
## attr(,"class")
## [1] "waiver"
## 
## $tag_prefix
## list()
## attr(,"class")
## [1] "waiver"
## 
## $tag_suffix
## list()
## attr(,"class")
## [1] "waiver"
## 
## $tag_sep
## list()
## attr(,"class")
## [1] "waiver"
## 
## $theme
## list()
## attr(,"class")
## [1] "waiver"
## 
## attr(,"class")
## [1] "plot_annotation"
p1 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_histogram(bins = 4, fill = "#440154", alpha = 0.9) +  
    theme_nath() +
     labs(title = "",
     x = NULL,
     y = "contagem",
     fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none")

p2 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_histogram(bins = 12, fill = "#440154", alpha = 0.9) +  
    theme_nath() +
    labs(title = "",
     x = NULL,
     y = NULL,
     fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none")

p3 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_histogram(bins = 25, fill = "#440154", alpha = 0.9) +  
    theme_nath() +
    labs(title = "",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none")

p1 + p2 + p3 + 
patchwork::plot_annotation(title = 'Histogramas com bins = 4, 12 e 25') 

p1 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 1.5, fill = "#440154", alpha = 0.9) +  
    theme_nath() +
     labs(title = "",
     x = NULL,
     y = "densidade",
     fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none")


p2 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 0.6, fill = "#440154", alpha = 0.9) +  
    theme_nath() +
    labs(title = "",
     x = NULL,
     y = NULL,
     fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none")

p3 <- gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 0.3, fill = "#440154", alpha = 0.9) +  
    theme_nath() +
    labs(title = "",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none")

p1 + p2 + p3 + 
patchwork::plot_annotation(title = 'Densidades com ajuste bandwidth = 1.5, 0.7 e 0.4') 

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 1, fill = "#440154", alpha = 0.4) +  
    # geom_vline(aes(xintercept=mean(life_exp)), linetype="solid", color = "#21918c")+
    # geom_text(aes(x=mean(life_exp)-3, y = 0.04, label = paste("média =", round(mean(life_exp),0))),
    #           linetype="dashed", color = "#21918c") +
    # geom_vline(aes(xintercept=mean(life_exp) - sd(life_exp)), linetype="dashed")+
    # geom_vline(aes(xintercept=mean(life_exp) + sd(life_exp)), linetype="dashed")+
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          axis.text.y=element_blank(),
          legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 1, fill = "#440154", alpha = 0.4) +  
    geom_vline(aes(xintercept=mean(life_exp)), linetype="solid", color = "#21918c")+
    geom_text(aes(x=mean(life_exp)-3, y = 0.04, label = paste("média =", round(mean(life_exp),0))),
              linetype="dashed", color = "#21918c") +
    # geom_vline(aes(xintercept=mean(life_exp) - sd(life_exp)), linetype="dashed")+
    # geom_vline(aes(xintercept=mean(life_exp) + sd(life_exp)), linetype="dashed")+
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          axis.text.y=element_blank(),
         legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 1, fill = "#440154", alpha = 0.4) +  
  
    geom_vline(aes(xintercept=mean(life_exp)), linetype="solid", color = "#21918c")+
    geom_text(aes(x=mean(life_exp)-3, y = 0.04, label = paste("média =", round(mean(life_exp),0))),
              linetype="dashed", color = "#21918c") +
  
    geom_vline(aes(xintercept=mean(life_exp) - sd(life_exp)), linetype="dashed", color = "#31688e")+
    geom_text(aes(x=mean(life_exp)- sd(life_exp)-1, y = 0.04, 
                  label = round(round(mean(life_exp),0) - sd(life_exp),0)),
              linetype="dashed", color = "#31688e") +

    geom_vline(aes(xintercept=mean(life_exp) + sd(life_exp)), linetype="dashed", color = "#31688e")+
    geom_text(aes(x=mean(life_exp)+ sd(life_exp)+1, y = 0.04, 
                  label = round(mean(life_exp) + sd(life_exp),0)),
              linetype="dashed", color = "#31688e") +
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          axis.text.y=element_blank(),
         legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 1, fill = "#440154", alpha = 0.4) +  
  
    geom_vline(aes(xintercept=mean(life_exp)), linetype="solid", color = "#21918c")+
    geom_text(aes(x=mean(life_exp)-3, y = 0.04, label = paste("média =", round(mean(life_exp),0))),
              linetype="dashed", color = "#21918c") +
  
    geom_vline(aes(xintercept=mean(life_exp) - sd(life_exp)), linetype="dashed", color = "#31688e")+
    geom_text(aes(x=mean(life_exp)- sd(life_exp)-1, y = 0.04, 
                  label = round(round(mean(life_exp),0) - sd(life_exp),0)),
              linetype="dashed", color = "#31688e") +

    geom_vline(aes(xintercept=mean(life_exp) + sd(life_exp)), linetype="dashed", color = "#31688e")+
    geom_text(aes(x=mean(life_exp)+ sd(life_exp)+1, y = 0.04, 
                  label = round(mean(life_exp) + sd(life_exp),0)),
              linetype="dashed", color = "#31688e") +
  
    geom_vline(aes(xintercept=min(life_exp)), linetype="dotted", color = "darkgrey")+
    geom_text(aes(x=min(life_exp)+1, y = 0.043, 
                  label = round(min(life_exp),0)),
              linetype="dashed", color = "darkgrey") +
  
    
    geom_vline(aes(xintercept=max(life_exp)), linetype="dotted", color = "darkgrey")+
    geom_text(aes(x=max(life_exp)-1, y = 0.043, 
                  label = round(max(life_exp),0)),
              linetype="dashed", color = "darkgrey") +
  
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          axis.text.y=element_blank(),
         legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 1, fill = "#440154", alpha = 0.4) +  
  
    geom_vline(aes(xintercept=median(life_exp)), linetype="solid", color = "#21918c")+
    geom_text(aes(x=median(life_exp) - 4, y = 0.04, 
                  label = paste("mediana = ", round(median(life_exp),0))),
              linetype="dashed", color = "#21918c") +
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          axis.text.y=element_blank(),
         legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 1, fill = "#440154", alpha = 0.4) +  
  
    geom_vline(aes(xintercept=median(life_exp)), linetype="solid", color = "#21918c")+
    geom_text(aes(x=median(life_exp) - 4, y = 0.04, 
                  label = paste("mediana = ", round(median(life_exp),0))),
              linetype="dashed", color = "#21918c") +
  
    geom_vline(aes(xintercept=quantile(life_exp, 0.25)), linetype="dashed", color = "#31688e")+
    geom_text(aes(x=quantile(life_exp, 0.25)-1, y = 0.04, 
                  label = round(quantile(life_exp, 0.25),0)),
              linetype="dashed", color = "#31688e") +

    geom_vline(aes(xintercept=quantile(life_exp, 0.75)), linetype="dashed", color = "#31688e")+
    geom_text(aes(x=quantile(life_exp, 0.75)+1, y = 0.04, 
                  label = round(quantile(life_exp, 0.75),0)),
              linetype="dashed", color = "#31688e") +
  
  
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          axis.text.y=element_blank(),
         legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp))  +
    geom_density(adjust = 1, fill = "#440154", alpha = 0.4) +  
    geom_vline(aes(xintercept=mean(life_exp)), linetype="solid", color = "#21918c")+
    geom_text(aes(x=mean(life_exp)-3, y = 0.04, label = paste("média =", round(mean(life_exp),0))),
              linetype="dashed", color = "#21918c") +
  
    geom_vline(aes(xintercept=median(life_exp)), linetype="solid", color = "#fde725")+
    geom_text(aes(x=median(life_exp) + 4, y = 0.03, 
                  label = paste("mediana = ", round(median(life_exp),0))),
              linetype="dashed", color = "#fde725") +
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          axis.text.y=element_blank(),
         legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp, y =1))  +
    # geom_violin(fill = "#440154", color = "#440154", alpha = 0.7) +  
    geom_violin(width=1, fill = "#440154", color = "#440154", alpha = 0.4) +
    geom_boxplot(width=0.2, fill = "white", color="#440154", alpha=0.4) +
    # geom_text(aes(x=median(life_exp) - 4, y = 0.04, 
    #               label = paste("mediana = ", round(median(life_exp),0))),
    #           linetype="dashed", color = "#21918c") +
    # 
    # geom_text(aes(x=quantile(life_exp, 0.25)-1, y = 0.04, 
    #               label = round(quantile(life_exp, 0.25),0)),
    #           linetype="dashed", color = "#31688e") +
    # 
    # geom_text(aes(x=quantile(life_exp, 0.75)+1, y = 0.04, 
    #               label = "3º quartil" ),
    #           linetype="dashed", color = "#31688e") +
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países, ano de 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
          axis.text.y=element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          legend.position = "none")

Continentes

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c("1962", "1977", "2007")) %>% 
  mutate(year = forcats::fct_rev(as_factor(year))) %>% 
  ggplot(aes(x = life_exp, y = year))  +
    ggridges::geom_density_ridges(fill = "#440154", alpha = 0.4, scale = 0.85) +
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida dos países por ano",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    scale_x_continuous(limits = c(20, 90)) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          #axis.text.y=element_blank(),
          legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2007") %>%
  ggplot(aes(x = life_exp, y = forcats::fct_rev(continent), color = continent, fill = continent))  +
    ggridges::geom_density_ridges(alpha = 0.8) +
    scale_fill_brewer(palette = "Set2") +
    scale_color_brewer(palette = "Set2") +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida por continente, ano de 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    theme_nath() +
    scale_x_continuous(limits = c(20, 90)) +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "1962") %>%
  ggplot(aes(x = life_exp, y = forcats::fct_rev(continent), color = continent, fill = continent))  +
    ggridges::geom_density_ridges(alpha = 0.8) +
    scale_fill_brewer(palette = "Set2") +
    scale_color_brewer(palette = "Set2") +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida por continente, ano de 1962",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    scale_x_continuous(limits = c(20, 90)) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c("1962", "2007")) %>% 
  ggplot(aes(x = life_exp, y = forcats::fct_rev(continent), 
             color = interaction(year,continent), fill = interaction(year,continent)))  +
    ggridges::geom_density_ridges(alpha = 0.4) +
    scale_fill_manual(values = c("#66C2A5", "#388C72", "#FC8D62", "#FA4F0A", 
                                  "#8DA0CB","#4964A1", "#E78AC3","#D42C94", 
                                  "#A6D854", "#77A927")) +
    scale_color_manual(values = c("#66C2A5", "#388C72", "#FC8D62", "#FA4F0A", 
                                  "#8DA0CB","#4964A1", "#E78AC3","#D42C94", 
                                  "#A6D854", "#77A927")) +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida por continente, 1962 e 2007",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    scale_x_continuous(limits = c(20, 90)) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none")

## Region

gapminder_full %>% 
  filter(year == 2007) %>% 
  group_by(region) %>%
  select(life_exp) %>% 
  skimr::skim()
Data summary
Name Piped data
Number of rows 183
Number of columns 2
_______________________
Column type frequency:
numeric 1
________________________
Group variables region

Variable type: numeric

skim_variable region n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
life_exp Australia and New Zealand 0 1 80.72 0.73 80.20 80.46 80.72 80.98 81.24 ▇▁▁▁▇
life_exp Caribbean 0 1 72.63 5.32 60.92 70.42 73.03 76.53 78.75 ▂▁▃▇▆
life_exp Central Africa 0 1 65.53 NA 65.53 65.53 65.53 65.53 65.53 ▁▁▇▁▁
life_exp Central America 0 1 73.98 3.13 70.20 71.47 74.22 76.14 78.78 ▇▂▁▇▂
life_exp Central Asia 0 1 57.89 12.30 43.83 53.50 63.17 64.92 66.67 ▃▁▁▁▇
life_exp Eastern Africa 0 1 54.00 9.91 42.08 48.16 52.52 58.04 76.44 ▇▇▂▁▂
life_exp Eastern Asia 0 1 76.20 6.40 66.80 71.54 78.51 81.09 82.60 ▅▂▁▅▇
life_exp Eastern Europe 0 1 73.49 2.49 68.86 72.74 73.34 75.11 76.49 ▂▁▇▂▅
life_exp Melanesia 0 1 67.13 7.10 57.23 63.57 68.77 70.04 76.06 ▃▃▁▇▃
life_exp Micronesia 0 1 68.53 NA 68.53 68.53 68.53 68.53 68.53 ▁▁▇▁▁
life_exp Middle Africa 0 1 49.83 4.92 42.73 46.03 50.54 52.51 56.74 ▇▃▇▃▇
life_exp Northern Africa 0 1 70.21 5.83 58.56 71.21 71.82 73.52 73.95 ▂▁▁▁▇
life_exp Northern America 0 1 79.45 1.70 78.24 78.84 79.45 80.05 80.65 ▇▁▁▁▇
life_exp Northern Europe 0 1 78.77 3.19 71.37 78.75 79.37 80.37 81.76 ▂▁▁▇▆
life_exp Polynesia 0 1 72.94 1.34 71.45 72.38 73.31 73.68 74.06 ▇▁▁▇▇
life_exp South America 0 1 72.70 3.67 65.55 71.42 72.89 75.11 78.55 ▃▂▇▇▃
life_exp South-Eastern Asia 0 1 71.01 6.80 59.72 70.62 71.69 74.25 79.97 ▅▁▇▅▅
life_exp Southeast Asia 0 1 62.07 NA 62.07 62.07 62.07 62.07 62.07 ▁▁▇▁▁
life_exp Southern Africa 0 1 47.04 5.66 39.61 42.59 49.34 50.73 52.91 ▃▃▁▃▇
life_exp Southern Asia 0 1 66.93 3.28 63.78 64.54 65.55 69.09 72.40 ▇▂▂▁▃
life_exp Southern Europe 0 1 77.45 2.48 74.00 75.30 77.93 79.46 80.94 ▇▅▅▅▅
life_exp Western Africa 0 1 55.19 7.85 42.57 47.96 56.37 59.59 71.68 ▆▁▇▂▁
life_exp Western Asia 0 1 73.08 5.73 59.55 71.94 73.78 76.13 80.75 ▂▁▂▇▅
life_exp Western Europe 0 1 80.13 0.89 79.41 79.52 79.80 80.45 81.70 ▇▁▂▁▂
life_exp NA 0 1 69.46 2.76 65.47 67.17 69.03 71.97 72.96 ▂▇▂▂▇
gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year %in% c("1962", "1977", "2007")) %>% 
  filter(continent == "Ásia") %>% 
  mutate(year = forcats::fct_rev(as_factor(year))) %>% 
  ggplot(aes(x = life_exp, y = year))  +
    ggridges::geom_density_ridges(alpha = 0.8, color = "#4964A1", fill = "#8DA0CB") +
    # scale_fill_manual(values = c("#A6D854", "#77A927")) +
    # scale_color_manual(values = c("#A6D854", "#77A927")) +
    theme_nath() +
    labs(title = "",
         subtitle = "Distribuição da expectativa de vida da Ásia por ano",
         caption = "Source: Gapminder.org",
         x = NULL,
         y = NULL,
         fill = NULL) +
    scale_x_continuous(limits = c(20, 90)) +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", colour = "lightgrey", size = 0.2),
          #axis.text.y=element_blank(),
          legend.position = "none")

boxplot

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == "2002") %>%
  #filter(continent == "Oceania") %>%
  #filter(country == "Australia") %>%
  # group_by(year, country, continent) %>% 
  # summarise(
  #   life_exp = mean(life_exp, na.rm=T),
  #   fertility = mean(fertility, na.rm=T)
  # ) %>% 
  # ungroup() %>% 
  #mutate(year = as_factor(year)) %>% 
  ggplot(aes(x = life_exp, y = continent, color = continent))  +
    #ggplot(aes(x = continent, y = n, group = year, fill = year)) + 
    #geom_col(position = "dodge", colour = "white", size = 0.2, alpha = 0.8) +
    #scale_y_continuous(labels = scales::comma, expand = c(0, 0)) +
    #stat_summary(geom="point", fun.data = ~mean(.x,na.rm=T)) +
    geom_boxplot() +
    #geom_smooth(method = "glm") +
    #facet_grid(. ~ country, scales = "free_x") +
    #scale_fill_brewer(palette = "Set2") +
    scale_color_brewer(palette = "Set2") +
    # labs(title = "",
    #      subtitle = "Contagem de Países por Continente, de 1952 a 2002",
    #      caption = "Source: Gapminder.org",
    #      x = NULL,
    #      y = NULL,
    #      fill = NULL) +
    theme_nath() +
    theme(panel.grid.major.x = element_blank(),
         legend.position = "none")

Rascunhos

Treemap

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == 2002) %>%
  mutate(gdp_percap = pop * gdp_percap) %>% 
  group_by(year, country, continent) %>%
  summarise(
    life_exp = mean(life_exp, na.rm=T),
    fertility = mean(fertility, na.rm=T),
    gdp_percap = mean(gdp_percap, na.rm=T)
  ) %>%
  ungroup() %>%
  ggplot(aes(area = gdp_percap, fill = country, subgroup = continent, label = country)) +
    treemapify::geom_treemap() +
    treemapify::geom_treemap_subgroup_border(colour = "grey10") +
    treemapify::geom_treemap_subgroup_text(fontface = "bold", colour = "#f0f0f0", 
                                           alpha = 0.7, place = "bottomleft") +
    treemapify::geom_treemap_text(colour = "white", place = "centre", reflow = TRUE) +
    #scale_fill_brewer(palette = "Set2") +
    labs(title = "",
         subtitle = "Country GDP by continent, 2002",
         caption = "Source: Gapminder.org",
         x = NULL, 
         y = NULL, 
         fill = NULL) +
    theme_nath() +
    theme(legend.position = "none")

gapminder_full %>% 
  filter(continent != "FSU") %>% 
  filter(year == 2002) %>%
  mutate(gdp_percap = pop * gdp_percap) %>% 
  group_by(year, country, continent) %>%
  summarise(
    life_exp = mean(life_exp, na.rm=T),
    fertility = mean(fertility, na.rm=T),
    gdp_percap = mean(gdp_percap, na.rm=T)
  ) %>%
  ungroup() %>%
  ggplot(aes(area = gdp_percap, fill = continent, subgroup = continent, label = country)) +
    treemapify::geom_treemap() +
    treemapify::geom_treemap_subgroup_border(colour = "grey10") +
    treemapify::geom_treemap_subgroup_text(fontface = "bold", colour = "#f0f0f0", 
                                           alpha = 0.7, place = "bottomleft") +
    treemapify::geom_treemap_text(colour = "white", place = "centre", reflow = TRUE) +
    scale_fill_brewer(palette = "Set2") +
    labs(title = "",
         subtitle = "Country GDP by continent, 2002",
         caption = "Source: Gapminder.org",
         x = NULL, 
         y = NULL, 
         fill = NULL) +
    theme_nath() +
    theme(legend.position = "none")

Lollipop + América

gapminder_full %>%
  filter(continent != "FSU") %>% 
  filter(year == 2007) %>% 
  filter(continent == "América") %>%
  #filter(region %in% c("Canada", "United States", "Dominican Republic", "Netherlands Antilles")) %>%
  ggplot(aes(life_exp, fct_reorder(region, life_exp))) + 
    geom_point(color = "#FC8D62", alpha = 0.8) +
    theme_nath() +
    labs(title = "",
       subtitle = "Expectativa de Vida dos países do continente Americano, 2007",
         caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
        legend.position = "none")

gapminder_full %>%
  filter(continent != "FSU") %>% 
  filter(year == 2002) %>% 
  filter(continent == "América") %>%
  #filter(region %in% c("Canada", "United States", "Dominican Republic", "Netherlands Antilles")) %>%
  ggplot(aes(life_exp, fct_reorder(region, life_exp))) + 
    geom_point(color = "#FC8D62", alpha = 0.8) +
    geom_point(data = . %>% filter(country == "Brazil"), color = "mediumpurple4", alpha = 0.8) +
    geom_text(data = . %>% filter(country == "Brazil"), aes(label = country),
              color = "mediumpurple4", alpha = 0.8, vjust = -0.8, size = 4) +
    theme_nath() +
    labs(title = "",
       subtitle = "Expectativa de Vida dos países do continente Americano, 2007",
       caption = "Source: Gapminder.org",
       x = NULL,
       y = NULL,
       fill = NULL) +
    theme(panel.grid.major.x = element_blank(),
        legend.position = "none")